package com.zds.a_controller;

import com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap;
import com.zds.y_entity.ResultEntity;
import com.zds.y_entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @className:UserController
 * @date: 2025/1/15 下午11:00
 * @description:
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @ResponseBody
    @PostMapping("/add")
    public ResultEntity add(@RequestBody User user) {
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setCode(200);
        resultEntity.setMsg("添加成功");
        resultEntity.setData(user);
        return resultEntity;

    }

    @GetMapping("/findall")
    @Transactional()
    @ResponseBody
    public List<User> findAll() {
//        User user = new User();
//        user.setId(1);
//        user.setName("钟定山");
//        user.setAge(18);
//        List<User> list = new ArrayList<>();
//        list.add(user);


//        List<User> list = jdbcTemplate.query("select * from tb_user",(rs,rownum)->{
//            User user = new User();
//            user.setId(rs.getInt("id"));
//            user.setName(rs.getString("name"));
//            user.setAge(rs.getInt("age"));
//            user.setDsc(rs.getString("profession"));
//            return user;
//        });
        List<User> list = jdbcTemplate.query("select * from tb_user",new BeanPropertyRowMapper<>(User.class));
        return list;
    }

    @PutMapping("/updateUser")
    public ResultEntity updateUser(@RequestBody User user) {
        ResultEntity result = new ResultEntity("更新成功", ResultEntity.CODE_OK);
        return result;
    }

}
